/*
 * Copyright 2025 The JA-SIG Collaborative. All rights reserved.
 * distributed with this file and available online at
 */
package com.lap.auth.domain.menu;

import com.lap.auth.domain.menu.valueobject.MenuType;
import com.lap.auth.shared.errors.UserError;
import com.lap.framework.common.ddd.AggregateRoot;
import com.lap.framework.common.exception.BizException;
import java.io.Serializable;
import java.util.List;
import lombok.Getter;

/**
 * 菜单领域。
 *
 * @author Shuisheng Lao(劳水生)
 * @version 0.0.1
 */
@Getter
public class Menu extends AggregateRoot implements Serializable {

  private final Integer id;
  private final Integer parentId;
  private final MenuType menuType;
  private final String name;
  private final String path;
  private final String component;
  private final String redirect;
  private final String auths;
  private final Integer rank;
  private final String title;
  private final String icon;
  private final String extraIcon;
  private final String enterTransition;
  private final String leaveTransition;
  private final String activePath;
  private final String frameSrc;
  private final Integer frameLoading;
  private final Integer showLink;
  private final Integer showParent;
  private final Integer keepAlive;
  private final Integer hiddenTag;
  private final Integer fixedTag;

  public Menu(
      Integer id,
      Integer parentId,
      MenuType menuType,
      String name,
      String path,
      String component,
      String redirect,
      String auths,
      Integer rank,
      String title,
      String icon,
      String extraIcon,
      String enterTransition,
      String leaveTransition,
      String activePath,
      String frameSrc,
      Integer frameLoading,
      Integer showLink,
      Integer showParent,
      Integer keepAlive,
      Integer hiddenTag,
      Integer fixedTag) {
    this.id = id;
    this.parentId = parentId;
    this.menuType = menuType;
    this.name = name;
    this.path = path;
    this.component = component;
    this.redirect = redirect;
    this.auths = auths;
    this.rank = rank;
    this.title = title;
    this.icon = icon;
    this.extraIcon = extraIcon;
    this.enterTransition = enterTransition;
    this.leaveTransition = leaveTransition;
    this.activePath = activePath;
    this.frameSrc = frameSrc;
    this.frameLoading = frameLoading;
    this.showLink = showLink;
    this.showParent = showParent;
    this.keepAlive = keepAlive;
    this.hiddenTag = hiddenTag;
    this.fixedTag = fixedTag;
  }

  public boolean isNotContainer() {
    return !this.menuType.isContainer();
  }

  public void assertCanChangeMenuType(MenuType type, List<Menu> child) {
    if (type == this.getMenuType()) {
      return;
    }
    if (isNotContainer() && !child.isEmpty()) {
      BizException.interrupt(UserError.MENU_CONTAINER);
    }
  }

  public void canCreateChild() {
    if (isNotContainer()) {
      BizException.interrupt(UserError.MENU_NOT_CONTAINER);
    }
  }

  public void canDelete(List<Menu> child) {
    if (!child.isEmpty()) {
      BizException.interrupt(UserError.MENU_HAS_CHILD);
    }
  }

  public boolean isNew() {
    return (id == null);
  }
}
